package com.hd123.rumba.qt.result;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;

import com.hd123.rumba.qt.def.QueryDefinition;
import com.hd123.rumba.qt.def.SQLOrder;
import com.hd123.rumba.qt.def.SQLOrderList;

public class DisplayCriteria implements Serializable {
  /**
   * 从params中查找key为qtSortColumn_number和qtSortOrder_number的entry,
   * 将其value放到orders中. number从0开始.
   * 
   * @param params
   *            QueryResultDescription
   */
  public void setCriteria(Hashtable params) {
    orders = new SQLOrderList();
    // 取得qtSortColumn的个数
    int count = 0;
    Enumeration keys = params.keys();
    while (keys.hasMoreElements()) {
      String key = (String) keys.nextElement();
      if (key.startsWith(PARAM_SORT_COLUMN_PREFIX))
        ++count;
    }
    // 按次序组建orders的项
    for (int i = 0; i < count; ++i) {
      String column = new String((String) params.get(PARAM_SORT_COLUMN_PREFIX + i));
      String order = new String((String) params.get(PARAM_SORT_ORDER_PREFIX + i));
      if (column != null && column.trim().length() > 0) {
        boolean found = false;
        for (int k = 0; k < orders.size(); k++)
          if (((SQLOrder) orders.get(k)).getColumn().equalsIgnoreCase(column)) {
            found = true;
            break;
          }
        if (!found) {
          SQLOrder o = new SQLOrder();
          o.setQuery(query);
          o.setColumn(column);
          o.setOrder(order == null ? "ASC" : order);
          o.setTitle(column);
          orders.add(o);
        }
      }
    }

    int rpp = getRowsPerPage().intValue();
    try {
      rpp = Integer.parseInt((String) params.get(PARAM_ROWS_PER_PAGE_KEY));
    } catch (Exception e) {
    }
    setRowsPerPage(new Integer(rpp));
  }

  /**
   * 将当前条件构造成一个hashtable, 满足setCriteria的参数规范
   * 
   * @return
   * @see QueryResultDescription#getCriteria()
   */
  public Hashtable getCriteria() {
    Hashtable h = new Hashtable();
    if (orders != null) {
      for (int i = 0; i < orders.size(); ++i) {
        SQLOrder o = (SQLOrder) orders.get(i);
        h.put(PARAM_SORT_COLUMN_PREFIX + i, o.getColumn() == null ? "" : query.getColumns()
            .getResultTableColumnNameByColumnTitle(o.getColumn()));
        h.put(PARAM_SORT_ORDER_PREFIX + i, o.getOrder() == null ? "" : o.getOrder());
      }
    }
    return h;
  }

  public String debugInfo() {
    StringBuffer sb = new StringBuffer();
    sb.append(getClass().getName() + "\n");
    sb.append("orders=" + (orders == null ? null : orders.debugInfo()) + "\n");
    sb.append("rowsPerPage=" + rowsPerPage + "\n");
    return sb.toString();
  }

  private transient QueryDefinition query;
  private SQLOrderList orders;
  private Integer rowsPerPage = new Integer(1000);

  public QueryDefinition getQuery() {
    return query;
  }

  public void setQuery(QueryDefinition query) {
    this.query = query;
  }

  public SQLOrderList getOrders() {
    return orders;
  }

  public void setOrders(SQLOrderList orders) {
    this.orders = orders;
  }

  public Integer getRowsPerPage() {
    return rowsPerPage;
  }

  public void setRowsPerPage(Integer rowsPerPage) {
    this.rowsPerPage = rowsPerPage;
  }

  public String getInfo() {
    StringBuffer sb = new StringBuffer();
    if (orders.size() > 0) {
      for (int i = 0; i < orders.size(); ++i) {
        SQLOrder o = (SQLOrder) orders.get(i);
        if (i > 0)
          sb.append(";");
        sb.append(o.getTitle().trim());
        sb.append(QueryDefinition.SORTORDERSVNM.get(o.getOrder()));
      }
      sb.append(";");
    }
    sb.append(rowsPerPage);
    sb.append("笔/页");
    return sb.toString();
  }

  public static final String PARAM_SORT_COLUMN_PREFIX = "qtSortColumn_";
  public static final String PARAM_SORT_ORDER_PREFIX = "qtSortOrder_";
  public static final String PARAM_ROWS_PER_PAGE_KEY = "qtRpp_";
}
